package com.fashionbrot.github.util;

import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.WeightedObservedPoints;

/**
 * 多项式拟合
 */
public class PolynomialUtil {


    public static void main(String[] args) {
        WeightedObservedPoints obs = new WeightedObservedPoints();

        String str = "1.79,-0.083464567\n" +
                "0.53,-0.075590551\n" +
                "0.48,-0.244094488\n" +
                "1.55,-0.152755906\n" +
                "0.97,-0.377952756\n" +
                "2.4,-0.426771654\n" +
                "2.71,0.503937008\n" +
                "-3.2,-0.048818898\n" +
                "0.31,0.699212598\n" +
                "-4.44,0.062992126\n" +
                "-0.4,0.708661417\n" +
                "-4.5,0.184251969\n" +
                "-1.17,0.511811024\n" +
                "-3.25,-0.165354331\n" +
                "1.05,0.264566929\n" +
                "-1.68,-0.354330709\n" +
                "2.25,0.548031496\n" +
                "-3.48,-0.050393701\n" +
                "0.32,0.837795276\n" +
                "-5.32,0.519685039\n" +
                "-3.3,-0.240944882\n" +
                "1.53,-0.582677165\n" +
                "3.7,0.022047244\n" +
                "-0.14,0.42992126\n" +
                "-2.73,-0.269291339\n" +
                "1.71,-0.113385827\n" +
                "0.72,-0.281889764\n" +
                "1.79,0.403149606\n" +
                "-2.56,-0.096062992\n" +
                "0.61,0.023622047\n" +
                "-0.15,0.201574803\n" +
                "-1.28,-0.018897638\n" +
                "0.12,-0.522834646\n" +
                "3.32,-0.077165354\n" +
                "0.49,-0.094488189\n" +
                "0.6,0.130708661\n" +
                "-0.83,-0.31023622\n" +
                "1.97,-0.313385827\n" +
                "1.99,0.177952756\n" +
                "-1.13,0.349606299\n" +
                "-2.22,-0.088188976\n" +
                "0.56,0.296062992\n" +
                "-1.88,0.256692913\n" +
                "-1.63,-0.193700787\n" +
                "1.23,-0.171653543\n" +
                "1.09,0.037795276\n" +
                "-0.24,-0.040944882\n" +
                "0.26,-0.272440945\n" +
                "1.73,-0.051968504\n" +
                "0.33,-0.108661417\n" +
                "0.69,0.051968504\n" +
                "-0.33,-0.137007874\n" +
                "0.87,0.154330709\n" +
                "-0.98,-0.245669291\n" +
                "1.56,-0.08976378\n" +
                "0.57,0.099212598\n" +
                "-0.63,0.08976378\n" +
                "-0.57,0.352755906\n" +
                "-2.24,0.280314961\n" +
                "-1.78,0.067716535\n" +
                "-0.43,-0.283464567\n" +
                "1.8,-0.173228346\n" +
                "1.1,-0.033070866\n" +
                "0.21,-0.25984252\n" +
                "1.65,-0.245669291\n" +
                "1.56,-0.022047244\n" +
                "0.14,0.094488189\n" +
                "-0.6,-0.08503937\n" +
                "0.54,0.113385827\n" +
                "-0.72,-0.270866142\n" +
                "1.72,-0.422047244\n" +
                "2.68,0.116535433\n" +
                "-0.74,-0.157480315\n" +
                "1,0.264566929\n" +
                "-1.68,0.096062992\n" +
                "-0.61,-0.037795276\n" +
                "0.24,0.296062992\n" +
                "-1.88,0.028346457\n" +
                "-0.18,-0.022047244\n" +
                "0.14,-0.034645669\n" +
                "0.22,0.248818898\n" +
                "-1.58,-0.018897638\n" +
                "0.12,-0.026771654\n" +
                "0.17,0.17007874\n" +
                "-1.08,0.08503937\n" +
                "-0.54,0.135433071\n" +
                "-0.86,0.004724409\n" +
                "-0.03,0.081889764\n" +
                "-0.52,-0.066141732\n" +
                "0.42,-0.100787402\n" +
                "0.64,0.127559055\n" +
                "-0.81,0.018897638\n" +
                "-0.12,-0.22992126\n" +
                "1.46,-0.053543307\n" +
                "0.34,0.062992126\n" +
                "-0.4,-0.045669291\n" +
                "0.29,-0.058267717\n" +
                "0.37,0.266141732\n" +
                "-1.69,0.02519685\n" +
                "-0.16,0.163779528\n" +
                "-1.04,-0.040944882\n" +
                "0.26,0.207874016\n";

        String[] split = str.split("\n");
        for(int i=0;i<split.length;i++){
            String[] aa = split[i].split(",");
            obs.add(Double.valueOf(aa[0]),Double.valueOf(aa[1]));
        }


        // Instantiate a third-degree polynomial fitter.
        final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(4);

        // Retrieve fitted parameters (coefficients of the polynomial function).
        final double[] coeff = fitter.fit(obs.toList());
        for (double c : coeff) {
            System.out.println(c);
        }
    }


}
